package me.goldze.mvvmhabit.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import me.goldze.mvvmhabit.net.RetrofitClient;


public class HtmlUtils {
    /**
     * 将html文本内容中包含img标签的图片，宽度变为屏幕宽度，高度根据宽度比例自适应
     **/
    public static String getNewContent(String htmltext){
        try {
            Document doc= Jsoup.parse(htmltext);
            Elements elements=doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width","100%").attr("height","auto");
            }
            Elements pngs = doc.select("img[src]");
            for (Element element : pngs) {
                String imgUrl = element.attr("src");
                if (imgUrl.trim().startsWith("/")) {
                    imgUrl = "https:" + imgUrl;
                    element.attr("src", imgUrl);
                }
            }
            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }
}